<template>
	<view class="meetingform">
		<view class="meetingtitle">{{title}}</view>
		<view class="formuserinfo">
			<form  class="formsubmit" >
				<view class="foronebox">
					<view class="userinfoval">
						单位名称<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.unitName" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						通讯地址<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.postalAddress" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						邮编<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.postalCode" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						传真<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.fax" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						发票抬头<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.invoiceTitle" class="form1input">
					<view class="showtitle">您的发票抬头将作为付费重要信息，请认真填写。</view>
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						纳税人识别号<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.taxpayerCode" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						联系人<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.contacts" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						部门<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.department" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						职务/职称<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.post" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						联系电话<text class="biao">*</text>
					</view>
					<input type="number" v-model="useobj.officePhone" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						手机号码<text class="biao">*</text>
					</view>
					<input type="number" v-model="useobj.phone" class="form1input">
				</view>
				<view class="foronebox">
					<view class="userinfoval">
						邮箱<text class="biao">*</text>
					</view>
					<input type="text" v-model="useobj.emil" class="form1input">
				</view>
			</form>
			<view class="otrtitle">参加培训人员名单</view>
			<view class="addbox">
				<block v-for="(item,index) in addarr" :key="index">
					<form  class="addmeeting" >
						<view class="foronebox">
							<view class="userinfoval">
								姓名<text class="biao">*</text>
							</view>
							<input type="text" v-model="item.name" class="form1input">
						</view>
						<view class="foronebox">
							<view class="userinfoval">
								性别<text class="biao">*</text>
							</view>
							<input type="text" v-model="item.gender" class="form1input">
						</view>
						<view class="foronebox">
							<view class="userinfoval">
								部门<text class="biao">*</text>
							</view>
							<input type="text" v-model="item.department" class="form1input">
						</view>
						<view class="foronebox">
							<view class="userinfoval">
								职务<text class="biao">*</text>
							</view>
							<input type="text" v-model="item.post" class="form1input">
						</view>
						
						<view class="foronebox">
							<view class="userinfoval">
								手机<text class="biao">*</text>
							</view>
							<input type="number" v-model="item.phone" class="form1input">
						</view>
						<view class="foronebox">
							<view class="userinfoval">
								邮箱<text class="biao">*</text>
							</view>
							<input type="text" v-model="item.emil" class="form1input">
						</view>
					</form>
				</block>
			</view>
			<view class="addbtn" v-if="addbtn" @click="addbtnfn">点击增加参会人员与人员信息</view>
			<view class="removebtn" v-if="removebtn" @click="removebtnfn">删除最后一个参会人员</view>
		</view>
		<view class="fixed">
			<view class="shenbao" @click="change" >提交信息</view>
		</view>
	</view>
</template>
<script>
	import {common} from '@/common/publish.js';
	export default {
		data() {
			return {
				meetingdata:{},
				user:{},
				useobj:{
					unitName:'',
					postalAddress:'',
					postalCode:'',
					fax:'',
					invoiceTitle:'',
					taxpayerCode:'',
					contacts:'',
					department:'',
					post:'',
					officePhone:'',
					phone:'',
					emil:''
				},
				addobj:{
					name:'',
					gender:'',
					department:'',
					post:'',
					phone:'',
					emil:''
				},
				addarr:[],
				addbtn:true,
				removebtn:false,
				even :{},
				title:''
			};
		},
		onLoad(event) {
			//signupid，meetingid
			this.even = event;
			this.gettitle(event.meetingid)
			this.getlist(event.signupid)
			this.getlist2(event.signupid)
			uni.getStorage({
				key: 'uerInfo',
				success: function (res) {
					this.user = res.data;
				}
			});
		},
		methods: {
			//获取title
			gettitle(val){
				uni.request({
					url: common.localurl+'/api/meeting/detail.json' , 
					data: {
						meetingId: val
					},
					success: (res) => {
						this.title = res.data.title
						uni.setNavigationBarTitle({
							title: res.data.title
						});
					}
				});
			},
			getlist(val){
				var that = this;
				uni.request({
					url: common.localurl+'/api/pc/toEdit.json' , 
					data: {
						signUpId: val
					},
					success: (res) => {
						this.useobj = res.data
					}
				});
			},
			getlist2(val){
				uni.request({
					url: common.localurl+'/api/pc/toAttEdit.json' , 
					data: {
						signUpId: val
					},
					success: (res) => {
						this.addarr = res.data;
						if(this.addarr.length>1){
							this.removebtn = true;
						}else{
							this.removebtn = false;
						}
						if(this.addarr.length>=5){
							this.addbtn = false;
						}else{
							this.addbtn = true;
						}
					}
				});
			},
			change(){
				var that = this;
				var istrue = true;
				var nub = 0;
				delete(that.useobj["openId"]);
				//首先判断useobj的内容是否为空
				for(var i in this.useobj){
					if(!this.useobj[i]){
						istrue = false;
						uni.showToast({
							title: '请将报名信息填写完整',
							icon:'none',
							duration: 1500
						});
						break;
					}
				}
				//判断添加报名的是否为空
				for(var i =0;i<this.addarr.length;i++){
					for(var j in this.addarr[i]){
						if(!this.addarr[i][j]){
							istrue = false;
							uni.showToast({
								title: '请将报名信息填写完整',
								icon:'none',
								duration: 1500
							});
							break;
						}
					}
				}
				if(istrue){
					//存储上半部分
					
					uni.request({
						url: common.localurl+'/api/pc/signUpEdit.json?signUpId='+that.even.signupid , 
						data: that.useobj,
						success: (res) => {
							//this.useobj = res.data
						}
					});
					for(var i=0;i<that.addarr.length;i++){
						if(that.addarr[i].id){
							//修改
							uni.request({
								url: common.localurl+'/api/pc/attEdit.json', 
								data: that.addarr[i],
								success: (res) => {
									nub++
									if(nub==that.addarr.length ){
										uni.redirectTo({
											url: '/pages/meetingpay/meetingpay?meetingid='+that.even.meetingid+'&signupid='+that.even.signupid
										});
									}
								}
							});
						}else{
							//添加
							uni.request({
								url: common.localurl+'/api/meeting/attSignUp.json?signUpId='+that.even.signupid, 
								data: that.addarr[i],
								success: (res) => {
									nub++
									if(nub==that.addarr.length ){
										uni.navigateTo({
											url: '/pages/meetingpay/meetingpay?meetingid='+that.even.meetingid+'&signupid='+that.even.signupid
										});
									}
								}
							});
						}
					}
					
				}
			},
			addbtnfn(){
				var newobj = {
					name:'',
					gender:'',
					department:'',
					post:'',
					phone:'',
					emil:''
				}
				if(this.addarr.length<5){
					this.addarr.push(newobj)
					this.removebtn = true;
				}else{
					this.addbtn = false;
				}
			},
			removebtnfn(){
				var that = this;
				if(that.addarr[that.addarr.length-1].id){
					uni.request({
						url: common.localurl+'/api/pc/attDelete.json' , 
						data: {
							attId: that.addarr[that.addarr.length-1].id
						},
						success: (res) => {
							that.getlist2(that.even.signupid)
						}
					});
				}else{
					 if(this.addarr.length>1){
					 this.addarr.splice(this.addarr.length-1, 1)
					}
					if(this.addarr.length == 1){
						this.removebtn = false;
					}else{
						this.removebtn = true;
					}
					if(this.addarr.length >= 5){
						this.addbtn = false;
					}else{
						this.addbtn = true;
					}
				}
			}
		},
	}
</script>

<style lang="scss">
	@import '@/common/publish.scss';
	page{
		background-color: $bgcolor;
	}
	.meetingtitle{
		padding:14upx 24upx;
		font-size:34upx;
		line-height:50upx;
		text-align:center;
		color:$titlecolor;
		border-bottom:1px solid #ccc;
	}
	.formuserinfo{
		padding:20upx 24upx;
		margin-bottom:100upx;
	}
	.foronebox{
		width:100%;
		margin-top:30upx;
	}
	.userinfoval{
		font-size:30upx;
		line-height:50upx;
		color:$timecolor;
		
		vertical-align:middle;
		
	}
	.biao{
		color:red;
		line-height:50upx;
		vertical-align:middle;
	}
	.form1input{
		box-sizing:border-box;
		width:702upx;
		display: block;
		height: 90upx;
		font-size: 28upx;
		line-height: 90upx;
		color: #555;
		padding:12upx 24upx;
		background-color: #fff;
		background-image: none;
		border: 1px solid #ccc;
		border-radius: 8upx;
	}
	.showtitle{
		text-align:center;
		font-size:28upx;
		line-height:40upx;
		color:red;
	}
	.addbtn{
		color: #fff;
		background-color: #337ab7;
		border-color: #2e6da4;
		margin:30upx auto;
		border-radius:8upx;
		font-size:32upx;
		line-height:80upx;
		height:80upx;
		text-align:center;
	}
	.removebtn{
		color: #fff;
		background-color: #d9534f;
		border-color: #d43f3a;
		margin:30upx auto;
		border-radius:8upx;
		font-size:32upx;
		line-height:80upx;
		height:80upx;
		text-align:center;
	}
	.fixed{
		position:fixed;
		left:0;
		bottom:0;
		width:750upx;
		height:80upx;
		background-color: #ccc;
		z-index:10;
	}
	.shenbao{
		position:absolute;
		top:0;
		right:0;
		text-align:center;
		height:100%;
		width:40%;
		background-color:#dd6264;
		color:#fff;
		line-height:80upx;
	}
	.otrtitle{
		text-align:center;
		font-size:36upx;
		border-bottom:1px solid #ccc;
		padding:20upx;
		padding-top:30upx;
	}
</style>
